package com.magic.smartcity.view.more.metro.home

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.Menu
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.magic.smartcity.R
import com.magic.smartcity.beans.MetroStepList
import com.magic.smartcity.databinding.FragmentMetroHomeLineDetailBinding
import com.magic.smartcity.view.MainActivity
import com.magic.smartcity.view.more.adapters.MetroHomeAllLineStepAdapter
import com.magic.smartcity.view.more.viewmodels.MetroViewModel

class MetroHomeLineDetailFragment : Fragment() {

    private lateinit var binding: FragmentMetroHomeLineDetailBinding

    private val metroHomeAllLineStepAdapter = MetroHomeAllLineStepAdapter()

    private val metroViewModel by lazy {
        ViewModelProvider(this).get(MetroViewModel::class.java)
    }

    private lateinit var  name:String
    private lateinit var nowlocation:String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        name = arguments?.getString("lineName")!!
        nowlocation = arguments?.getString("nowLocation")!!
        (activity as MainActivity).setActionBarTitle(name)
        val id = arguments?.getInt("id").toString()
        metroViewModel.getStepDetailByLineId(id)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentMetroHomeLineDetailBinding.inflate(layoutInflater)

        initView()

        return binding.root
    }

    private fun initView() {

        binding.allLineRv.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        binding.allLineRv.adapter = metroHomeAllLineStepAdapter

        //绑定列车信息

        metroViewModel.lineDetail.observe(viewLifecycleOwner) {
            binding.lineDetail = it
            binding.nowStation = nowlocation
            metroHomeAllLineStepAdapter.setData(it.metroStepList, it.runStationsName)
            binding.allLineRv.scrollToPosition(it.metroStepList.indexOfFirst { its ->
                its.name == it.runStationsName
            })
        }
        //


    }

}